package com.qianjiangcloud.store.activity.account;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianjiangcloud.store.R;
import com.qianjiangcloud.store.BaseDoFragment;
import com.qianjiangcloud.store.Run;
import com.qianjiangcloud.store.http.JsonRequestBean;
import com.qianjiangcloud.store.http.JsonTask;
import com.qianjiangcloud.store.http.JsonTaskHandler;


@SuppressLint("ResourceAsColor")
public class AccountLogisticsFragment extends BaseDoFragment {
    private TextView mLogisticsName;
    private TextView mLogisticsId;
    private LayoutInflater mInflater;
    private String strId;
	private View contain;
    private List<JSONObject> infoArray = new ArrayList<JSONObject>();
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
     mActionBar.setTitle(R.string.account_logitics_info_title);
	 Intent intent=mActivity.getIntent();
	 strId=intent.getStringExtra("orderId");
	}
  
	@Override
	public void init(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater=inflater;
		rootView=inflater.inflate(R.layout.fragment_logistics,null);
		Run.excuteJsonTask(new JsonTask(), new GetLogisticsTask(strId));
		
	}
//加载每一个订单
	private void loadLogisticsView(JSONObject object,View parent,LayoutInflater inf){
		infoArray.clear();
		contain=inf.inflate(R.layout.fragment_logistics_main, null);
		mLogisticsName=(TextView)contain.findViewById(R.id.account_logistics_name);
		mLogisticsId = (TextView)contain.findViewById(R.id.account_logistics_id);
		mLogisticsName.setText("快递公司：" + object.optString("company"));
		mLogisticsId.setText("运单编号：" + object.optString("number"));
		JSONArray msgArray=object.optJSONArray("info");
		if(msgArray!=null&&msgArray.length()>0){
			for(int i=0;i<msgArray.length();i++) {
				addItemView(msgArray.optJSONObject(i), i, contain);

			}
		}
		((ViewGroup)parent.findViewById(R.id.linter_contain)).addView(contain);
	}
	//加载订单中每一项的没一个地址信息
	public void addItemView(JSONObject data,int position,View contain){
		View convertView =  mInflater.inflate(R.layout.view_route_item, null);
		ImageView icon =  (ImageView) convertView.findViewById(R.id. iv_route_icon );
		View icon_top_line =  convertView.findViewById(R.id.icon_top_line);
		View icon_bottom_line =  convertView.findViewById(R.id.icon_bottom_line );
		LinearLayout ll_bottom_line =  (LinearLayout) convertView.findViewById(R.id.ll_bottom_line );
		TextView info =  (TextView) convertView.findViewById(R.id.tv_route_info );
		info.setAutoLinkMask(Linkify.ALL);
		TextView time =  (TextView) convertView.findViewById(R.id.tv_route_time );
		TextView address =  (TextView) convertView.findViewById(R.id.tv_route_address);
		info.setTextColor(Color.parseColor("#999999"));
		time.setTextColor(Color.parseColor("#999999"));
		address.setTextColor(Color.parseColor("#999999"));
		if (position == 0) {
			icon.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.logistics_track_arrive));
			icon_top_line.setVisibility(View.INVISIBLE);
			icon_bottom_line.setVisibility(View.VISIBLE);
			ll_bottom_line.setVisibility(View.VISIBLE);
			info.setTextColor(Color.parseColor("#f04641"));
			time.setTextColor(Color.parseColor("#f04641"));
			address.setTextColor(Color.parseColor("#f04641"));
		}else if (position == infoArray.size()-1) {
			icon.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.logistics_track_point));
			icon_bottom_line.setVisibility(View.INVISIBLE);
			ll_bottom_line.setVisibility(View.INVISIBLE);
			icon_top_line.setVisibility(View.VISIBLE);
		}else {
			icon.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.logistics_track_point));
			icon_top_line.setVisibility(View.VISIBLE);
			icon_bottom_line.setVisibility(View.VISIBLE);
			ll_bottom_line.setVisibility(View.VISIBLE);
		}

		time.setText(data.optString("AcceptTime"));
		address.setText(data.optString("AcceptStation"));
		((ViewGroup)contain.findViewById(R.id.contain_item)).addView(convertView);

	}


	@Override
	public void onClick(View v) {

		super.onClick(v);
	}

	private class GetLogisticsTask implements JsonTaskHandler {
		private String orderId;
		public GetLogisticsTask(String orderID) {
			orderId = orderID;
		}

	
		public void task_response(String json_str) {
			hideLoadingDialog_mt();
			try {
				JSONObject all = new JSONObject(json_str);
				if (Run.checkRequestJson(mActivity, all)){
				   JSONObject data=all.optJSONObject("data");
				   if(data!=null){
					//   contain.setVisibility(View.VISIBLE);
					   JSONArray expArry=data.optJSONArray("exp");
					   if (expArry!=null&&expArry.length()>0){
						   for(int i=0;i<expArry.length();i++){
							   expArry.getJSONObject(i);
							   loadLogisticsView(expArry.getJSONObject(i), rootView, mInflater);
						   }
					   }

				   }
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		public JsonRequestBean task_request() {
			showCancelableLoadingDialog();
			JsonRequestBean req = new JsonRequestBean(
					"mobileapi.express.order_exp");
			req.addParams("order_id", orderId);
			return req;
		}

	}
}
